package com.jinhao.crowd.service.impl;

import com.jinhao.crowd.entity.Auth;
import com.jinhao.crowd.entity.AuthExample;
import com.jinhao.crowd.mapper.AuthMapper;
import com.jinhao.crowd.service.api.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created on 2021/10/3.
 *
 * @author Valar Morghulis
 */

@Service
public class AuthServiceImpl implements AuthService {

    @Autowired
    private AuthMapper authMapper;

    @Override
    public List<Auth> getAll() {
        return authMapper.selectByExample(new AuthExample());
    }

    @Override
    public List<Integer> getAssignedAuthIdByRoleId(Integer roleId) {

        List<Integer> authIdList = authMapper.selectAuthIdByRoleId(roleId);
        return authIdList;
    }

    @Override
    public void updateRoleAuthRelationship(Map<String, List<Integer>> map) {
        List<Integer> roleIdList = map.get("roleId");
        Integer roleId = roleIdList.get(0);
        // 删除旧的role-auth关系
        authMapper.deleteOldRelationship(roleId);

        List<Integer> authIdArray = map.get("authIdArray");
        if (authIdArray != null && authIdArray.size() > 0) {
            authMapper.addNewRelationship(roleId, authIdArray);
        }
    }


    @Override
    public List<String> getAssignedAuthNameByAdminId(Integer adminId) {

        List<String> authNameList = authMapper.selectAssignedAuthNameByAdminId(adminId);
        return authNameList;
    }
}
